package com.example.avplayer;

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.List;
import java.util.Map;

// Camera 老的相机
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
    private final String TAG = "CameraSurfaceView";
    private Camera mCamera;
    SurfaceHolder mHolder;
    //预览宽高    yuv  有多大   是1  不是2
    byte[] buffer;
    private Camera.Size previewSize;
    private H264CameraEncode h264Encode;

    public CameraSurfaceView(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHolder = getHolder();
        mHolder.addCallback(this);


    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHolder = getHolder();
        mHolder.addCallback(this);
    }


    public void openCamera() {
        if (mCamera==null) {


        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //默认预览图像是镜像的,这里根据设备的方向做一个旋转
        final int orientation = getResources().getConfiguration().orientation;
        int previewCaptureRotation = 90;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            previewCaptureRotation = 180;
        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            previewCaptureRotation = 90;
        }
        mCamera.setDisplayOrientation(previewCaptureRotation);
        Camera.Parameters parameters = mCamera.getParameters();
        //设置自动对焦,否则画面不清晰
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        mCamera.setParameters(parameters);
        previewSize = parameters.getPreviewSize();
        Log.e(TAG,"with:"+previewSize.width);
        Log.e(TAG,"height:"+previewSize.height);
        buffer = new byte[previewSize.width * previewSize.height * 3 / 2];
        }
        mCamera.addCallbackBuffer(buffer);
        mCamera.setPreviewCallbackWithBuffer(this);
        // 预览到 surface
//        连接预览 - 使用 Camera.setPreviewDisplay() 将 SurfaceView 连接到相机，做好实时相机图片预览准备。
//        开始预览 - 调用 Camera.startPreview() 以开始显示实时相机图片。
        mCamera.startPreview();
    }

    public void  stop() {
        if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }


    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        openCamera();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
           openCamera();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // Camera 得字节码
        Log.d(TAG,"数据回调");
        if (h264Encode == null) {
            try {
                this.h264Encode = new H264CameraEncode(previewSize.width, previewSize.height);
            } catch (IOException e) {
                e.printStackTrace();

            }

//            h264Encode.startLive();
        }
//        数据
        h264Encode.encodeFrame(data);
        //处理完图像也必须再调用一次addCallbackBuffer()函数，否则后面就不会回调了
        mCamera.addCallbackBuffer(data);
    }
}
